package com.zoke.loveway.utils;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zoke.loveway.R;
import com.zoke.support.base.BaseActivity;
import com.zoke.support.base.BaseFragment;

/**
 * 处理页面状态管理类
 * 
 * @author 大熊
 *
 */
public class PageState {

	protected BaseActivity mBaseAct;
	protected BaseFragment mBaseFragment;
	protected Context mContext;
	private AnimationDrawable mAnima;

	public PageState(BaseActivity baseAct) {
		this.mBaseAct = baseAct;
		mContext = baseAct;
	}

	public PageState(BaseFragment baseFragment) {
		this.mBaseFragment = baseFragment;
		mContext = baseFragment.getActivity();
	}

	/** 获取PageState的容器 **/
	public View getContainer() {
		return mBaseAct == null ? mBaseFragment
				.findViewById(R.id.showStateContainer) : mBaseAct
				.findViewById(R.id.showStateContainer);
	}

	/** 暂时未登录 **/
	public void showUnlogin() {
		showContainer();
		getLoadingView().setVisibility(View.GONE);
		ImageView iv = getTipsImageView();
		TextView tip = getTipsTextView();
		TextView remark = getRemarkTextView();
		iv.setVisibility(View.VISIBLE);
		tip.setVisibility(View.VISIBLE);
		remark.setVisibility(View.VISIBLE);
		iv.setImageResource(R.drawable.pagestate_withoutdatas);
		tip.setText(getString(R.string.unlogin));
		remark.setText(getString(R.string.login));// 此时是按钮
		getRemarkContainer().setVisibility(View.VISIBLE);
		getRemarkContainer()
				.setBackgroundResource(R.drawable.pagestate_btn_bg);
		remark.setTextColor(getColor(R.color.pagestate_selector));
	}

	/** 获取Remakr的Container helpContainerLl **/
	public View getRemarkContainer() {
		return mBaseAct == null ? mBaseFragment
				.findViewById(R.id.helpContainerLl) : mBaseAct
				.findViewById(R.id.helpContainerLl);
	}

	/** 获取图片提示信息控件 **/
	public ImageView getTipsImageView() {
		return mBaseAct == null ? (ImageView) mBaseFragment
				.findViewById(R.id.showStateWithImage) : (ImageView) mBaseAct
				.findViewById(R.id.showStateWithImage);
	}

	/** 获取文本提示信息控件 **/
	public TextView getTipsTextView() {
		return mBaseAct == null ? (TextView) mBaseFragment
				.findViewById(R.id.showStateWithText) : (TextView) mBaseAct
				.findViewById(R.id.showStateWithText);
	}

	/** getLoadingView **/
	public ImageView getLoadingView() {
		ImageView loadingV = mBaseAct == null ? (ImageView) mBaseFragment
				.findViewById(R.id.showStateWithLoading) : (ImageView) mBaseAct
				.findViewById(R.id.showStateWithLoading);
		mAnima = (AnimationDrawable) loadingV.getDrawable();
		return loadingV;
	}

	/** 获取备注信息控件--主要用于提供一些操作步骤 **/
	public TextView getRemarkTextView() {
		return mBaseAct == null ? (TextView) mBaseFragment
				.findViewById(R.id.showStateHelpUser) : (TextView) mBaseAct
				.findViewById(R.id.showStateHelpUser);
	}

	/** 显示PageState 不建议直接使用 **/
	public void showContainer() {
		try {
			getContainer().setVisibility(View.VISIBLE);
			getRemarkContainer().setVisibility(View.GONE);
		} catch (Exception e) {
			LogTest.wlj("异常了");
		}
	}

	/** 隐藏PageState **/
	public void hide() {
		getContainer().setVisibility(View.GONE);
	}

	/**
	 * 网络加载失败
	 * 
	 * @param imageResId
	 *            提示图片的资源id
	 * @param tipsResId
	 *            提示内容的资源id
	 */
	public void showNetFailed(int imageResId, int tipsResId) {
		showContainer();
		handleView(imageResId, tipsResId, 0);
	}

	/**
	 * 网络加载失败默认值 -- 仅支持此方法
	 */
	public void showNetFailed() {
		showContainer();
		handleView(R.drawable.pagestate_net_failed, R.string.net_faild, 0);
		TextView mRemarkTv = getRemarkTextView();
		mRemarkTv.setVisibility(View.VISIBLE);
		mRemarkTv.setText(getString(R.string.netfailed_refresh));// 此时是按钮
		getRemarkContainer().setVisibility(View.VISIBLE);
		getRemarkContainer()
				.setBackgroundResource(R.drawable.pagestate_btn_bg);
		mRemarkTv.setTextColor(getColor(R.color.pagestate_selector));
	}

	/**
	 * 网络加载失败默认值
	 * 
	 * @param strResId
	 *            提示信息
	 */
	public void showNetFailed(int strResId) {
		showContainer();
		handleView(R.drawable.pagestate_net_failed, strResId, 0);
	}

	/** 根据内容处理View的显示和设置数据 **/
	private void handleView(int imageResId, int tipsResId, int remarkResId) {
		getLoadingView().setVisibility(View.GONE);
		ImageView mIv = getTipsImageView();
		mIv.setVisibility(imageResId == 0 ? View.GONE : View.VISIBLE);
		TextView mTipsTv = getTipsTextView();
		mTipsTv.setVisibility(tipsResId == 0 ? View.GONE : View.VISIBLE);
		TextView mRemarkTv = getRemarkTextView();
		mRemarkTv.setVisibility(remarkResId == 0 ? View.GONE : View.VISIBLE);
		// 设置图片内容
		if (imageResId != 0)
			mIv.setImageResource(imageResId);
		if (tipsResId != 0)
			mTipsTv.setText(getString(tipsResId));
		if (remarkResId != 0)
			mRemarkTv.setText(getString(remarkResId));
	}

	/**
	 * 网络加载失败
	 * 
	 * @param imageResId
	 *            提示图片的资源id 没有则隐藏
	 * @param tipsResId
	 *            提示内容的资源id 没有则隐藏
	 * @param remarkResId
	 *            备注内容，或者提示操作步骤的资源id 没有则隐藏
	 */
	public void showNetFailed(int imageResId, int tipsResId, int remarkResId) {
		showContainer();
		handleView(imageResId, tipsResId, remarkResId);
	}

	private String getString(int resId) {
		return mContext.getResources().getString(resId);
	}

	private int getColor(int resId) {
		return mContext.getResources().getColor(resId);
	}

	/**
	 * 界面没有数据的提示
	 * 
	 * @param imageResId
	 *            提示图片的资源id
	 * @param tipsResId
	 *            提示内容的资源id
	 * @param remarkResId
	 *            备注内容，或者提示操作步骤的资源id
	 */
	public void showWithoutDatas(int imageResId, int tipsResId, int remarkResId) {
		showContainer();
		handleView(imageResId, tipsResId, remarkResId);
	}

	/**
	 * 界面没有数据的提示
	 * 
	 */
	public void showWithoutDatas() {
		showContainer();
		handleView(R.drawable.pagestate_withoutdatas,
				R.string.default_withouttips, 0);
	}

	/**
	 * 界面没有数据的提示
	 * 
	 * @param tipsResId
	 *            提示内容的资源id
	 */
	public void showWithoutDatas(int tipsResId) {
		showContainer();
		handleView(R.drawable.pagestate_withoutdatas, tipsResId, 0);
	}

	/**
	 * 界面没有数据的提示
	 * 
	 * @param imageResId
	 *            提示图片的资源id
	 * @param tipsResId
	 *            提示内容的资源id
	 */
	public void showWithoutDatas(int imageResId, int tipsResId) {
		showContainer();
		handleView(imageResId, tipsResId, 0);
	}

	/**
	 * 正在加载中（界面内的加载，用户可操作其他内容）
	 * 
	 * @param imageResId
	 *            loading动画的资源id
	 */
	public void showLoading(int imageResId, int tipsResId) {
		showContainer();
		getRemarkTextView().setVisibility(View.GONE);
		TextView tipsTv = getTipsTextView();
		ImageView mIv = getTipsImageView();
		mIv.setVisibility(View.GONE);
		tipsTv.setVisibility(View.VISIBLE);
		tipsTv.setText(getString(tipsResId));
		getLoadingView().setVisibility(View.VISIBLE);
		if (mAnima != null)
			mAnima.start();
	}

	/**
	 * 正在加载界面
	 * 
	 * @param tipsResId
	 */
	public void showLoading(int tipsResId) {
		showContainer();
		getTipsTextView().setVisibility(View.GONE);
		getRemarkTextView().setVisibility(View.GONE);
		TextView tipsTv = getTipsTextView();
		ImageView mIv = getTipsImageView();
		mIv.setVisibility(View.GONE);
		tipsTv.setVisibility(View.VISIBLE);
		tipsTv.setText(getString(tipsResId));
		getLoadingView().setVisibility(View.VISIBLE);
		if (mAnima != null)
			mAnima.start();
	}

	/**
	 * 正在加载中（界面内的加载，用户可操作其他内容）
	 * 
	 * @param imageResId
	 *            loading动画的资源id
	 */
	public void showLoading() {
		showContainer();
		getTipsTextView().setVisibility(View.GONE);
		getRemarkTextView().setVisibility(View.GONE);
		ImageView mIv = getTipsImageView();
		mIv.setVisibility(View.GONE);
		getLoadingView().setVisibility(View.VISIBLE);
		if (mAnima != null)
			mAnima.start();
	}

}
